using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;
namespace LdExpress
{
    /// <summary>
    /// TinhTP
    /// </summary>
    public class Countries
    {
        public long ID = -1;
        public string CountryCode = "";
        public string CountryName = "";
        public string ContinentName = "";
        public string Continent = "";
        public long ServiceAreaID = 0;
        public Countries()
        {
        }
        public Countries(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                strSQL = "INSERT INTO `tbl_countries` " +
                        "(" +
                         "`CountryCode`, " +
                         "`CountryName`, " +
                         "`ContinentName`, " +
                         "`Continent`," +
                         "`ServiceAreaID`" +
                         ") VALUES(" +
                         "'" + DBModule.RefineString(CountryCode) + "'," +
                         "'" + DBModule.RefineString(CountryName) + "'," +
                         "'" + DBModule.RefineString(ContinentName) + "'," +
                         "'" + DBModule.RefineString(Continent) + "'," +
                         ServiceAreaID.ToString() +
                         ")";
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Countries", "add", cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update tbl_countries set " +
                             "CountryCode='" + DBModule.RefineString(CountryCode) + "'," +
                             "CountryName='" + DBModule.RefineString(CountryName) + "'," +
                             "ContinentName='" + DBModule.RefineString(ContinentName) + "'," +
                             "Continent='" + DBModule.RefineString(Continent) + "'" + "," +
                             "ServiceAreaID=" + ServiceAreaID +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Countries", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            //Buil SQL Statement
            string strSQL = "DELETE FROM tbl_countries WHERE ID = " + this.ID.ToString();
            //Run SQL Statement
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("Countries", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT * FROM tbl_countries WHERE ID = " + ID.ToString() + " limit 0,1";
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("CountryCode")) CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("CountryName")) CountryName = dr["CountryName"].ToString();
                if (!dr.IsNull("ContinentName")) ContinentName = dr["ContinentName"].ToString();
                if (!dr.IsNull("Continent")) Continent = dr["Continent"].ToString();
                if (!dr.IsNull("ServiceAreaID")) ServiceAreaID = long.Parse(dr["ServiceAreaID"].ToString());
            }
        }
        public void Load(string countryCode, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT * FROM tbl_countries WHERE CountryCode='" + countryCode + "' limit 0,1";
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("CountryCode")) CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("CountryName")) CountryName = dr["CountryName"].ToString();
                if (!dr.IsNull("ContinentName")) ContinentName = dr["ContinentName"].ToString();
                if (!dr.IsNull("Continent")) Continent = dr["Continent"].ToString();
            }
        }
        public void LoadByName(string countryName, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            //Build SQL Statement
            strSQL = "SELECT * FROM tbl_countries WHERE CountryName='" + countryName + "'";
            //Run SQL Statement
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("CountryCode")) CountryCode = dr["CountryCode"].ToString();
                if (!dr.IsNull("CountryName")) CountryName = dr["CountryName"].ToString();
                if (!dr.IsNull("ContinentName")) ContinentName = dr["ContinentName"].ToString();
                if (!dr.IsNull("Continent")) Continent = dr["Continent"].ToString();
            }
        }
        public static void GetList(string strCondition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_countries where 1=1 ";
            if ((strCondition != null) && (strCondition != ""))
            {
                strSQL += strCondition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public static void DoSearch(string strCondition, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_countries where 1=1 ";
            if (strCondition.Length > 0)
            {
                strSQL += strCondition;
            }

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}
